package com.pig4cloud.pig.module.controller.product;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.product.ProductAttrResult;
import com.pig4cloud.pig.module.service.product.IProductAttrResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 商品属性详情Controller
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Validated
@Api(value = "商品属性详情控制器", tags = {"商品属性详情管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/product/attr/result")
public class ProductAttrResultController {

    private final IProductAttrResultService productAttrResultService;

    /**
     * 查询商品属性详情列表
     */
    @ApiOperation("查询商品属性详情列表")
    @GetMapping("/list")
    public TableDataInfo<ProductAttrResult> list(@Validated ProductAttrResult productAttrResult) {
        return PageUtils.buildDataInfo(productAttrResultService.page(PageUtils.buildPage()));
    }

    /**
     * 获取商品属性详情详细信息
     */
    @ApiOperation("获取商品属性详情详细信息")
    @GetMapping("/{id}")
    public R<ProductAttrResult> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(productAttrResultService.getById(id));
    }

    /**
     * 新增商品属性详情
     */
    @ApiOperation("新增商品属性详情")
    @SysLog("商品属性详情")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody ProductAttrResult productAttrResult) {
        return R.okByRow(productAttrResultService.save(productAttrResult) ? 1 : 0);
    }

    /**
     * 修改商品属性详情
     */
    @ApiOperation("修改商品属性详情")
    @SysLog("商品属性详情")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody ProductAttrResult productAttrResult) {
        return R.okByRow(productAttrResultService.updateById(productAttrResult) ? 1 : 0);
    }

    /**
     * 删除商品属性详情
     */
    @ApiOperation("删除商品属性详情")
    @SysLog("商品属性详情")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return R.okByRow(productAttrResultService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
